
var api = require('../../utils/api.js');
var app = getApp();
// 引入富文本解析组件
var WxParse = require('../../wxParse/wxParse.js');
Page({
    data: {
        tophe:0
    },
    currentPageNumber: 1,
    onLoad(ret) {
        this.getinfobyid(ret.id);

        var file=api.FIlE;
        this.setData({
            bg:file
        })
    },
    onShow() {
        let jn = wx.getMenuButtonBoundingClientRect();

        console.log(jn);
        var tophe=jn.top+jn.height+15;
        this.setData({
            tophe:tophe+'px',
            over:jn.top
        })
     
    },

    // 获取详情
    getinfobyid(id){
        var that=this;
        api.post({
            url: 'zixun/getinfobyid',
            data: {id:id},
            success: data => {
                // 初始化FAQ项的显示状态
                if (data.data && data.data.faqs && data.data.faqs.length > 0) {
                    data.data.faqs.forEach(item => {
                        item.showAnswer = false; // 默认隐藏答案
                    });
                }
                
                // 处理富文本内容
                if (data.data && data.data.content) {
                    // 修复图片显示问题
                    let content = data.data.content;
                    
                    // 1. 确保图片路径是完整的
                    content = content.replace(/<img[^>]*src="(?!http)(\/[^"]+)"/gi, function(match, src) {
                        // 将相对路径转换为绝对路径
                        return match.replace(src, api.HOST + src);
                    });
                    
                    // 2. 添加图片样式，确保图片宽度适应屏幕
                    content = content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;"');
              
                    // 设置富文本内容
                    that.setData({
                        zixun: data.data,
                        htmlSnip: that.formatRichText(content)
                    });
                    
                    // 同时使用WxParse作为备选方案
                    WxParse.wxParse('article', 'html', content, that, 5);
                } else {
                    that.setData({
                        zixun: data.data
                    });
                }
            },
        });
    },
    
    // 切换问题答案的显示状态
    toggleAnswer(e) {
        const index = e.currentTarget.dataset.index;
        const key = `zixun.faqs[${index}].showAnswer`;
        this.setData({
            [key]: !this.data.zixun.faqs[index].showAnswer
        });
    },
    back(){
        wx.navigateBack();
    },
    formatRichText(html) {
        //控制小程序中图片大小
        let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
            console.log(match.search(/style=/gi));
            if (match.search(/style=/gi) == -1) {
                match = match.replace(/\<img/gi, '<img style=""');
            }
            return match;
        });

        newContent = newContent.replace(/style="/gi, '$& max-width:100% !important; ');
        newContent = newContent.replace(/<br[^>]*\/>/gi, '');
        return newContent;
    },


});